package com.zz.springboot.processor.multiToOne;

import com.zz.springboot.exception.BizException;
import com.zz.springboot.processor.DataProcessor;
import com.zz.springboot.resp.enums.RespCodeEnum;
import com.zz.springboot.util.DataDealUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class MultiToOneProcessor implements DataProcessor {
    @Override
    public List<String> process(String data) {
        List<String> dataList = new ArrayList<>();
        //处理 13。12。30(200) 这类格式
        data= data.replace("(", "各").replace("（","各").
                replace(")","").replace("）","");

        //1）定位金额
        String[] dataArr = data.split("各");
        Float money = DataDealUtils.extractMoneyFromStr(dataArr[1]);
        //2）定位号码
        //①如“1/3/5各20米”或“1\3\5各20米”或“1,3,5各20米”或“1.3.5各20米”或“1、3、5各20米”，先全部转换为“1 3 5各20米”格式
        dataArr[0] = dataArr[0].replace("号"," ").replace("/", " ").replace("\\", " ").
                replace(",", " ").replace("，", " ").replace("、", " ").
                replace(".", " ").replace("。", " ").
                replace("+", " ").replace("-", " ").replace("—", " ").
                replace("*"," ").replace("~"," ");
        //②最后统一对“1 3 5各20米”格式处理，统一转化成json格式[1:20,3:20,5:20]
        String[] numArr = dataArr[0].split("\\s+");
        if (DataDealUtils.hasDuplicates(numArr)) {
            log.error("******本组号码[{}]中存在重复的号码，请先检查调整！！！", dataArr[0]);
            throw new BizException(RespCodeEnum.DUPLICATE_NUM.getErrCode(),data+"存在重复的号码");
        }
        //3）纳入集合
        for (String s : numArr) {
            if (!StringUtils.hasText(s)) {  //解决：“3/4/5/各10块”以“/”结尾以及其它会产生空字符串的格式；
                continue;
            }
            dataList.add(Integer.valueOf(s) + ":" + money);
        }
        return dataList;
    }
}
